home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFskyedit / c / PreQuit < prev    next >
Encoding:
Text File  |  2003-11-06  |  4.0 KB  |  136 lines

  1. /*
  2.  *  SFskyedit - Star Fighter 3000 sky colours editor
  3.  *  Quit confirm dbox
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24.  
  25. /* RISC OS library files */
  26. #include "event.h"
  27. #include "toolbox.h"
  28. #include "quit.h"
  29. #include "wimplib.h"
  30.  
  31. /* My library files */
  32. #include "err.h"
  33. #include "msgtrans.h"
  34. #include "Macros.h"
  35. #include "TboxBugs.h"
  36. #ifdef FOCUS_CRAP
  37. #include "InputFocus.h"
  38. #endif
  39. #include "viewsmenu.h"
  40.  
  41. /* Local headers */
  42. #include "PreQuit.h"
  43. #include "editsky.h"
  44.  
  45. static ObjectId dbox_id = NULL_ObjectId;
  46. static int quit_sender;
  47.  
  48. /* ----------------------------------------------------------------------- */
  49. /*                       Function prototypes                               */
  50.  
  51. static ToolboxEventHandler _PreQuit_quit_handler;
  52.  
  53. /* ----------------------------------------------------------------------- */
  54. /*                         Public functions                                */
  55.  
  56. void PreQuit_initialise(ObjectId PreQuit_id)
  57. {
  58.   /* Record ID */
  59.   dbox_id = PreQuit_id;
  60.  
  61.   /* Install handlers */
  62.   EF(event_register_toolbox_handler(PreQuit_id, Quit_Quit, _PreQuit_quit_handler, NULL));
  63. #ifdef FOCUS_CRAP
  64.   EF(event_register_toolbox_handler(PreQuit_id, Quit_AboutToBeShown, InputFocus_recordcaretpos, NULL));
  65. #endif
  66. }
  67.  
  68. /* ----------------------------------------------------------------------- */
  69.  
  70. bool TRY_QUIT(int task_handle)
  71. {
  72.   ViewData *ihatethiscode;
  73.   ObjectId editingwindow;
  74.   int unsaved_count = 0;
  75.   char number[16];
  76.  
  77.   editingwindow = ViewMenu_getfirst();
  78.   while(editingwindow != NULL_ObjectId) {
  79.     if(!E(toolbox_get_client_handle(0, editingwindow, (void **)&ihatethiscode))) {
  80.       if(ihatethiscode->changed_since_save)
  81.         unsaved_count++;
  82.     }
  83.     editingwindow = ViewMenu_getnext(editingwindow);
  84.   }
  85.   
  86.   if(unsaved_count > 1) {
  87.     sprintf(number, "%d", unsaved_count);
  88.     RE(quit_set_message(0, dbox_id, msgs_lookup_sub1("PlurUNS", number)))
  89.   }
  90.   else {
  91.     if(unsaved_count == 1)
  92.       RE(quit_set_message(0, dbox_id, msgs_lookup("SingUNS")))
  93.     else
  94.       return true; /* safe to quit */
  95.   }
  96.   
  97.   quit_sender = task_handle;
  98.   RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, dbox_id, Toolbox_ShowObject_Centre, NULL, NULL_ObjectId, NULL_ComponentId))
  99.   return false; /* don't want to quit */
  100. }
  101.  
  102. /* ----------------------------------------------------------------------- */
  103. /*                         Private functions                               */
  104.  
  105. static int _PreQuit_quit_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  106. {
  107.   /* We won't be alive to hear the MenusDeleted msg, so fake it */
  108. #ifdef FOCUS_CRAP
  109.   RE(InputFocus_restorecaret())
  110. #endif
  111.   if(quit_sender != 0) {
  112.     /* Restart desktop shutdown */
  113.     WimpKeyPressedEvent key_event;
  114.     RE(wimp_get_caret_position(&key_event.caret))
  115.     key_event.key_code = 0x1FC;
  116.     RE(wimp_send_message(Wimp_EKeyPressed, &key_event, quit_sender, 0, NULL))
  117.  
  118. #ifdef QUIT_ON_SHUTDOWN
  119.     /* Quit immediately */
  120.     exit(EXIT_SUCCESS);
  121. #else
  122.     /* Do as Paint, Edit and Draw do - that is, discard all data */
  123.     ObjectId editingwindow = ViewMenu_getfirst();
  124.     while(editingwindow != NULL_ObjectId) {
  125.       RE(toolbox_delete_object(0, editingwindow))
  126.       editingwindow = ViewMenu_getnext(editingwindow);
  127.     }
  128. #endif
  129.  
  130.   } else {
  131.     /* Quit application */
  132.     exit(EXIT_SUCCESS);
  133.   }
  134.   return 1; /* claim event */
  135. }
  136.